package com.eastfair.mobile.service.impl;

//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.request.PageParams;
import com.eastfair.common.dto.AttachmentFileDTO;
import com.eastfair.common.vo.AttachmentFileVO;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.mobile.enums.MobileMoudleEnum;
import com.eastfair.mobile.service.MobileViolationRecordService;
import com.eastfair.pay.api.AttachmentFileFeign;
import com.eastfair.venuebooking.api.ServiceProviderCreditFeign;
import com.eastfair.venuebooking.api.ViolationRecordFeign;
import com.eastfair.venueservice.dto.ServiceProviderCreditDTO;
import com.eastfair.venueservice.dto.ViolationRecordDTO;
import com.eastfair.venueservice.vo.ServiceProviderCreditVO;
import com.eastfair.venueservice.vo.ViolationRecordVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @Author lyw
 * @Date 2022/9/13 20:26
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class MobileViolationRecordServiceImpl implements MobileViolationRecordService {

    @Autowired
    private ViolationRecordFeign violationRecordFeign;
    @Autowired
    private ServiceProviderCreditFeign serviceProviderCreditFeign;
    @Autowired
    private AttachmentFileFeign attachmentFileFeign;

    /**
     * 保存违规惩罚记录
     *
     * @param violationRecordDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveViolationRecordInfo(ViolationRecordDTO violationRecordDTO) {
        violationRecordDTO.setViolationRecordEnum(MobileMoudleEnum.VIOLATION_RECORD.getCode());
        R<Boolean> r = violationRecordFeign.saveForHome(violationRecordDTO);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        ServiceProviderCreditDTO serviceProviderCreditDTO = new ServiceProviderCreditDTO();
        serviceProviderCreditDTO.setServiceProviderId(violationRecordDTO.getSupervisedId());

        //查询服务商信息
        R<ServiceProviderCreditVO> serviceProvider = serviceProviderCreditFeign.getServiceProviderCreditById(violationRecordDTO.getSupervisedId());
        if (!serviceProvider.getIsSuccess()) {
            throw BizException.wrap(serviceProvider.getCode(), serviceProvider.getMsg());

        }
        if (Objects.nonNull(serviceProvider) && Objects.nonNull(serviceProvider.getData())) {
            serviceProviderCreditDTO.setScore(serviceProvider.getData().getScore() - violationRecordDTO.getScore());
            serviceProviderCreditDTO.setId(serviceProvider.getData().getId());
        }
        //修改服务商星级分
        R<Boolean> b = serviceProviderCreditFeign.updateServiceProviderCreditInfo(serviceProviderCreditDTO);
        if (!b.getIsSuccess()) {
            throw BizException.wrap(b.getCode(), b.getMsg());
        }
        return r.getData();
     }

    /**
     * 查询违规惩罚记录列表分页
     *
     * @param params
     * @return
     */
    @Override
    public Page<ViolationRecordVO> queryViolationRecordList(PageParams<ViolationRecordDTO> params) {
        R<Page<ViolationRecordVO>> r = violationRecordFeign.queryViolationRecordList(params);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        Page<ViolationRecordVO> resultPage = ConvertUtil.convertPage(r.getData(), ViolationRecordVO.class);
        if(Objects.nonNull(resultPage) && Objects.nonNull(resultPage.getRecords())){
            resultPage.getRecords().forEach((s)->{
                //获取违规附件
                AttachmentFileDTO attachmentFileDTO =new AttachmentFileDTO();
                attachmentFileDTO.setBusinessId(s.getId());
                attachmentFileDTO.setBusinessType(MobileMoudleEnum.VIOLATION_RECORD.getCode());
                R<List<AttachmentFileVO>> listR = attachmentFileFeign.queryAttachmentFile(attachmentFileDTO);
                s.setAttachmentFileVOList(listR.getData());
            });
        }

        return resultPage;
    }

    /**
     * 查询违规惩罚记录详情
     *
     * @param violationRecordDTO
     * @return
     */
    @Override
    public ViolationRecordVO queryViolationRecordInfo(ViolationRecordDTO violationRecordDTO) {
        R<ViolationRecordVO> r = violationRecordFeign.getViolationRecord(violationRecordDTO);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        //获取违规附件
        AttachmentFileDTO attachmentFileDTO =new AttachmentFileDTO();
        attachmentFileDTO.setBusinessId(violationRecordDTO.getId());
        attachmentFileDTO.setBusinessType(MobileMoudleEnum.VIOLATION_RECORD.getCode());
        R<List<AttachmentFileVO>> listR = attachmentFileFeign.queryAttachmentFile(attachmentFileDTO);
        if(Objects.nonNull(listR.getData()) && listR.getData().size()>0){
            r.getData().setAttachmentFileVOList(listR.getData());
        }

        return r.getData();
    }

    /**
     * 修改违规惩罚记录
     * @param violationRecordDTO
     * @return
     */
    @Override
    public Boolean updateViolationRecordInfo(ViolationRecordDTO violationRecordDTO) {
        violationRecordDTO.setViolationRecordEnum(MobileMoudleEnum.VIOLATION_RECORD.getCode());
        R<Boolean> r = violationRecordFeign.updateViolationRecord(violationRecordDTO);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        ServiceProviderCreditDTO serviceProviderCreditDTO = new ServiceProviderCreditDTO();
        serviceProviderCreditDTO.setServiceProviderId(violationRecordDTO.getSupervisedId());

        //查询服务商信息
        R<ServiceProviderCreditVO> serviceProvider = serviceProviderCreditFeign.getServiceProviderCreditById(violationRecordDTO.getSupervisedId());
        if (!serviceProvider.getIsSuccess()) {
            throw BizException.wrap(serviceProvider.getCode(), serviceProvider.getMsg());

        }
        if (Objects.nonNull(serviceProvider) && Objects.nonNull(serviceProvider.getData())) {
            serviceProviderCreditDTO.setScore(serviceProvider.getData().getScore() - violationRecordDTO.getScore());
            serviceProviderCreditDTO.setId(serviceProvider.getData().getId());
        }
        //修改服务商星级分
        R<Boolean> b = serviceProviderCreditFeign.updateServiceProviderCreditInfo(serviceProviderCreditDTO);
        if (!b.getIsSuccess()) {
            throw BizException.wrap(b.getCode(), b.getMsg());
        }
        return r.getData();
    }

    /**
     * 删除违规惩罚记录
     * @param id
     * @return
     */
    @Override
    public Boolean delViolationRecordInfo(Long id) {
        R<Boolean> r = violationRecordFeign.delViolationRecord(id);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        return r.getData();
    }
}
